package org.ch02.test09.demo03;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 读取/写入JavaBean对象中的某一个属性
 *
 * @author CSY
 * @date 2025 - 03 - 25
 */
public class MainTest02 {
    public static void main(String[] args) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
        // 实例化 Person 对象 - JavaBean 对象
        Person bean = new Person("zing", 18);

        //获取PropertyDescriptor对象获取指定的属性 - new PropertyDescriptor("属性名",指定类的Class对象)
        PropertyDescriptor propertyDescriptor = new PropertyDescriptor("name",Person.class);

        //获取到setter
        Method writeMethod = propertyDescriptor.getWriteMethod();
        //执行setter
        writeMethod.invoke(bean,"张三");

        //获取到getter
        Method readMethod = propertyDescriptor.getReadMethod();
        //执行getter
        Object getName = readMethod.invoke(bean);
        System.out.println(getName);
    }
}
